
package com.yadong.numberguess;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Game {
    public static int [] SIMPLE_GAME_PARAMETER = new int [] {4, 10};
    public static int[] HARD_GAME_PARAMETER = new int [] {5, 12};
    public enum GameType {
        SIMPLE, HARD
    }

    private int mLength;
    private int mMax;
    private List<Integer> mRealNumber;
    private int mGuessTime = 0;

    public Game(GameType type) {
        int parameter[] = null;
        if (type == GameType.HARD) {
            parameter = HARD_GAME_PARAMETER;
        } else {
            parameter = SIMPLE_GAME_PARAMETER;
        }
        mLength = parameter[0];
        mMax = parameter[1];
        mRealNumber = new ArrayList<Integer>(mLength);
    }

    static class Result {
        int time;
        String input;
        String reminder;
        boolean success;

        public Result(int time, String input, String reminder,
                boolean success) {
            this.time = time;
            this.input = input;
            this.reminder = reminder;
            this.success = success;
        }
    }

    public String getRealNumber() {
        if (mRealNumber == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for(int i : mRealNumber) {
            switch (i) {
                case 10:
                    stringBuilder.append("x");
                    break;
                case 11:
                    stringBuilder.append("y");
                    break;
                case 12:
                    stringBuilder.append("z");
                    break;
                default:
                    stringBuilder.append(i);
                    break;
            }
        }
        return stringBuilder.toString();
    }

    public int getLength() {
        return mLength;
    }

    public int getInputMax() {
        return mMax;
    }

    public void reset() {
        mRealNumber.clear();
        Random random = new Random();
        while (mRealNumber.size() != mLength) {
            int i = random.nextInt(mMax);
            if (mRealNumber.indexOf(i) == -1) {
                mRealNumber.add(i);
            }
        }
        mGuessTime = 0;
    }

    public Result guess(String input) {
        mGuessTime++;
        int a = 0;
        int b = 0;
        try {
            for (int i = 0; i < mLength; i++) {
                int item = 0;
                String itemStr = input.substring(i, i + 1);
                try {
                    item = Integer.parseInt(itemStr);
                } catch (NumberFormatException exception) {
                    if (itemStr.toLowerCase().equals("x")) {
                        item = 10;
                    } else if (itemStr.toLowerCase().equals("y")) {
                        item = 11;
                    } else if (itemStr.toLowerCase().equals("z")) {
                        item = 12;
                    }
                }
                if (mRealNumber.get(i) == item) {
                    a++;
                } else if (mRealNumber.indexOf(item) != -1) {
                    b++;
                }
            }
        } catch (Exception e) {
        }
        return new Result(mGuessTime, input, a + "A" + b + "B", a == mLength);
    }
}
